package com.shire.shire_interface.service.invite;

import com.shire.shire_interface.dao.invite.WxInviteDayRecordDao;
import com.shire.shire_interface.dao.invite.WxInviteSettingDao;
import com.shire.shire_interface.model.invite.WxInviteDayRecord;
import com.shire.shire_interface.model.invite.WxInviteDayRecordExample;
import com.shire.shire_interface.model.invite.WxInviteSetting;
import com.shire.shire_interface.model.invite.WxInviteSettingExample;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 作者:  sll
 * 时间:  2019/8/8 17:23
 */
@Service
@CacheConfig(cacheNames = {"myCache"})
public class WxInviteSettingService {
    @Resource
    private WxInviteSettingDao wxInviteSettingDao;

    @Resource
    private WxInviteDayRecordDao inviteDayRecordDao;

    public Integer insertInviteSetting(WxInviteSetting wxInviteSetting){
        return wxInviteSettingDao.insertSelective(wxInviteSetting);
    }

    @Cacheable(key = "targetClass+'' + #p0 + #p1")
    public WxInviteSetting getInviteSetting(Integer csId, String wxAccount){
        WxInviteSettingExample example = new WxInviteSettingExample();
        example.createCriteria().andWxaccountidEqualTo(wxAccount);
        List<WxInviteSetting> settings = wxInviteSettingDao.selectByExample(example);
        if (settings.size() > 0)
            return settings.get(0);
        return null;
    }

    @CachePut(key = "targetClass + '' + #wxInviteSetting.csid + #wxInviteSetting.wxaccountid")
    public WxInviteSetting updateInviteSetting(WxInviteSetting wxInviteSetting){
        wxInviteSettingDao.updateByPrimaryKey(wxInviteSetting);
        return wxInviteSetting;
    }


    public WxInviteSetting getDefaultSetting(){
        WxInviteSetting wxInviteSetting = new WxInviteSetting();
        wxInviteSetting.setIntervalstart(10);
        wxInviteSetting.setIntervalend(30);
        wxInviteSetting.setDaylimit(30);
        wxInviteSetting.setSleepstart("22:00:00");
        wxInviteSetting.setSleepend("07:00:00");
        wxInviteSetting.setValidationmsg("你好");
        return wxInviteSetting;
    }

    @CachePut(key = "targetClass+'WxInviteDayRecord-' + #dayRecord.csid+'-' + #dayRecord.wxaccountid")
    public WxInviteDayRecord updateInviteDayRecord(WxInviteDayRecord dayRecord){
        inviteDayRecordDao.updateByPrimaryKeySelective(dayRecord);
        return dayRecord;
    }
    @CachePut(key = "targetClass+'WxInviteDayRecord-' + #dayRecord.csid+'-' + #dayRecord.wxaccountid")
    public WxInviteDayRecord insertInviteDayRecord(WxInviteDayRecord dayRecord){
        inviteDayRecordDao.insertSelective(dayRecord);
        return dayRecord;
    }

    @Cacheable(key = "targetClass+'WxInviteDayRecord-' + #p0+'-' + #p1")
    public WxInviteDayRecord getInviteDayRecordForAccount(Integer csId, Integer accountId, String strDate){
        WxInviteDayRecordExample dayRecordExample = new WxInviteDayRecordExample();
        //DateUtil.Date2String(new Date())
        dayRecordExample.createCriteria().andCsidEqualTo(csId).andWxaccountidEqualTo(accountId).andSenddateEqualTo(strDate);
        List<WxInviteDayRecord> dayRecordList = inviteDayRecordDao.selectByExample(dayRecordExample);

        if (dayRecordList.size() < 1)
            return null;
        return dayRecordList.get(0);
    }
}
